package com.jiuyun.Servlet;

import com.google.gson.Gson;
import com.jiuyun.dao.ClassesDao;
import com.jiuyun.dao.impl.ClassesDaoImpl;
import com.jiuyun.entity.Classes;
import com.jiuyun.entity.HomeWork;
import com.jiuyun.entity.Student;
import com.jiuyun.service.ClassService;
import com.jiuyun.service.Impl.ClassServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/class/*")
public class HomeworkServlet extends BaseServlet {

    ClassesDao dao = new ClassesDaoImpl();
    ClassService service = new ClassServiceImpl();
    Gson gson = new Gson();

    protected void work(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        HashMap info = null;
        String className=request.getParameter("className");
        Classes classes=dao.findclass(className);
        String data=request.getParameter("data");
        int year=Integer.parseInt(data.substring(0,4));
        int month=Integer.parseInt(data.substring(5,7));
        System.out.println(classes.getClassId()+"--"+year+"--"+month);
        try {
            info = service.homework(classes.getClassId(),year, month);
            System.out.println(service);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.setAttribute("homework", info);
        request.getRequestDispatcher("/class.jsp").forward(request, response);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int stid = Integer.parseInt(request.getParameter("stid"));
        int classid = Integer.parseInt(request.getParameter("classid"));
        String stname = request.getParameter("stname");
        String stpass = request.getParameter("stpass");
        try {
            int add = dao.getpupil(stid, classid, stname, stpass);
            System.out.println(add);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        request.getRequestDispatcher("/student.jsp").forward(request, response);
    }

    //查出所有班级信息 返回json
    protected void ban(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List list = dao.getClasses();
        String s = gson.toJson(list);
        //如何让servlet返回json数据
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        //输出流
        PrintWriter out = response.getWriter();
        out.println(s);  //数据
        out.flush();
        out.close();
    }

    //学生信息
    protected void xue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //班级id
        String cid = request.getParameter("cid");
        List stus = dao.getstudent(cid);
        String s = gson.toJson(stus);
        //如何让servlet返回json数据
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        //输出流
        PrintWriter out = response.getWriter();
        out.println(s);  //数据
        out.flush();
        out.close();
    }
    protected void addwork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        PrintWriter out = response.getWriter();
        int stid=Integer.parseInt(request.getParameter("stid"));
        String data=request.getParameter("riqi");
        String bz=request.getParameter("yy");
        System.out.println(stid+"--"+data+"--"+bz);
        int num=dao.addhomework(stid,data,bz);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        out.print("{\"code\":"+num+"}");  //数据
        out.flush();
        out.close();
    }
}
